package ThreadRunableVolite;

/**
 * 
 * @author zybw-kf01
 *如果我们希望 B 在 A 全部打印 完后再开始打印呢？我们可以利用 thread.join() 方法
 */
public class ThreadOrder2 {

	public static void main(String[] args) {

		Thread aThread = new Thread(new Runnable() {

			@Override
			public void run() {

				printNumber("A");
			}
		});

		Thread bThread = new Thread(new Runnable() {

			@Override
			public void run() {

				System.out.println("B 开始等待 A");
				try {
					aThread.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("B 开始打印");
				printNumber("B");
			}
		});

		aThread.start();
		bThread.start();

	}

	private static void printNumber(String threadName) {
		int i = 0;

		while (i++ < 3) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(threadName + " print: " + i);
		}
	}
}
